package com.mall.user.domain.model;

import com.mall.goods.common.enums.GoodsPromotionType;
import com.mall.goods.common.helper.ShopHelper;
import com.mall.goods.common.util.Common;
import com.mall.goods.common.util.State;
import com.mall.goods.common.config.ShopConfig;
import com.mall.user.domain.command.GiftVo;

import io.micrometer.common.util.StringUtils;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**

 *
 * 商品SKU
 * 
 *
 * Created 2017/4/13 11:20
 */
@Data
public class Goods implements Serializable,Cloneable {
    /**
     * 商品SKU编号
     */
    private int goodsId;
    /**
     * 商品SPU
     */
    private int commonId;
    /**
     * 规格<br>
     * 例“红色,,,L”
     */
    private String goodsSpecs="";
    /**
     * 规格字符串
     */
    private String goodsSpecString = "";
    /**
     * 完整规格<br>
     * 例“颜色：红色；尺码：L”
     */
    private String goodsFullSpecs="";
    /**
     * 所选规格值编号<br>
     * 列 “1,2,3,4”
     */
    private String specValueIds="";
    /**
     * 商品价格0
     */
    private BigDecimal goodsPrice0 = BigDecimal.ZERO;
    /**
     * 商品价格1
     */
    private BigDecimal goodsPrice1 = BigDecimal.ZERO;
    /**
     * 商品价格2
     */
    private BigDecimal goodsPrice2 = BigDecimal.ZERO;
    /**
     * PC端起购价0
     */
    private BigDecimal webPrice0 = BigDecimal.ZERO;
    /**
     * PC端起购价1
     */
    private BigDecimal webPrice1 = BigDecimal.ZERO;
    /**
     * PC端起购价2
     */
    private BigDecimal webPrice2 = BigDecimal.ZERO;
    /**
     * PC端促销进行状态
     */
    private int webUsable = 0;
    /**
     * APP端起购价0
     */
    private BigDecimal appPrice0 = BigDecimal.ZERO;
    /**
     * APP端起购价1
     */
    private BigDecimal appPrice1 = BigDecimal.ZERO;
    /**
     * APP端起购价2
     */
    private BigDecimal appPrice2 = BigDecimal.ZERO;
    /**
     * APP端促销进行状态
     */
    private int appUsable = 0;
    /**
     * 微信端起购价0
     */
    private BigDecimal wechatPrice0 = BigDecimal.ZERO;
    /**
     * 微信端起购价1
     */
    private BigDecimal wechatPrice1 = BigDecimal.ZERO;
    /**
     * 微信端起购价2
     */
    private BigDecimal wechatPrice2 = BigDecimal.ZERO;
    /**
     * 微信端促销进行状态
     */
    private int wechatUsable = 0;
    /**
     * 限时折扣促销编号
     */
    private int promotionId = 0;
    /**
     * 促销开始时间
     */
    private Timestamp promotionStartTime=new Timestamp(0);
    /**
     * 促销结束时间
     */
    private Timestamp promotionEndTime=new Timestamp(0);
    /**
     * 活动状态
     */
    private int promotionState = 0;
    /**
     * 活动类型
     */
    private int promotionType = GoodsPromotionType.GENERAL;
    /**
     * 活动类型文字
     */
    private String promotionTypeText="";
    /**
     * 折扣标题
     */
    private PromotionTitle promotionTitle= PromotionTitle.of("");
    /**
     * 商品货号
     */
    private String goodsSerial="";
    /**
     * 颜色规格值编号<br>
     * 编号为1的规格对应的规格值的编号
     */
    private int colorId=0;
    /**
     * 库存
     */
    private int goodsStorage=0;
    /**
     * 图片名称
     */
    private String imageName="";
    /**
     * 图片路径
     */
    private String imageSrc="";
    /**
     * 赠品列表
     */
    private List<GiftVo> giftVoList = new ArrayList<>();
    /**
     * 是否有赠品
     */
    private Integer isGift;
    /**
     * 拼团商品标记
     */
    private Integer isGroup;
    /**
     * 拼团价格
     */
    private BigDecimal groupPrice = BigDecimal.ZERO;
    /**
     * 限购数量
     */
    private int limitAmount = 0;
    /**
     * 秒杀商品标记
     */
    private int isSeckill;

    public String getGoodsSpecString() {
        if (StringUtils.isBlank(goodsSpecs)) {
            return "";
        } else {
            return goodsSpecs.replace(",,,", "；");
        }
    }

    public BigDecimal getWebPrice0() {
        if (getWebUsable() == State.NO) {
            return goodsPrice0;
        } else {
            return webPrice0;
        }
    }

    public BigDecimal getWebPrice1() {
        if (getWebUsable() == State.NO) {
            return goodsPrice1;
        } else {
            return webPrice1;
        }
    }

    public BigDecimal getWebPrice2() {
        if (getWebUsable() == State.NO) {
            return goodsPrice2;
        } else {
            return webPrice2;
        }
    }

    public int getWebUsable() {
        if (webUsable == State.NO || promotionState == State.NO || promotionStartTime.after(ShopHelper.getCurrentTimestamp()) || promotionEndTime.before(ShopHelper.getCurrentTimestamp())) {
            return State.NO;
        } else {
            return State.YES;
        }
    }

    public BigDecimal getAppPrice0() {
        if (getAppUsable() == State.NO) {
            return goodsPrice0;
        } else {
            return appPrice0;
        }
    }

    public BigDecimal getAppPrice1() {
        if (getAppUsable() == State.NO) {
            return goodsPrice1;
        } else {
            return appPrice1;
        }
    }

    public BigDecimal getAppPrice2() {
        if (getAppUsable() == State.NO) {
            return goodsPrice2;
        } else {
            return appPrice2;
        }
    }

    public int getAppUsable() {
        if (appUsable == State.NO || promotionState == State.NO || promotionStartTime.after(ShopHelper.getCurrentTimestamp()) || promotionEndTime.before(ShopHelper.getCurrentTimestamp())) {
            return State.NO;
        } else {
            return State.YES;
        }
    }

    public BigDecimal getWechatPrice0() {
        if (getWechatUsable() == State.NO) {
            return goodsPrice0;
        } else {
            return wechatPrice0;
        }
    }


    public BigDecimal getWechatPrice1() {
        if (getWechatUsable() == State.NO) {
            return goodsPrice1;
        } else {
            return wechatPrice1;
        }
    }

    public BigDecimal getWechatPrice2() {
        if (getWechatUsable() == State.NO) {
            return goodsPrice2;
        } else {
            return wechatPrice2;
        }
    }

    public int getWechatUsable() {
        if (wechatUsable == State.NO || promotionState == State.NO || promotionStartTime.after(ShopHelper.getCurrentTimestamp()) || promotionEndTime.before(ShopHelper.getCurrentTimestamp())) {
            return State.NO;
        } else {
            return State.YES;
        }
    }

    public String getPromotionTypeText() {
        return GoodsPromotionType.getText(promotionType);
    }


    public String getImageSrc() {
        if (imageName.equals("")) {
            return ShopConfig.getPublicRoot() + Common.DEFAULT_GOODS_IMG;
        } else {
            return ShopConfig.getUploadRoot() + imageName;
        }
    }

    public Integer getIsGift() {
        if (isGift == null) {
            return 0;
        }
        return isGift;
    }

    @Override
    public Goods clone() throws CloneNotSupportedException {
        return (Goods) super.clone();
    }
}
